#include <QFileDialog>
#include "mazeMainWin.h"

mazeMainWin::mazeMainWin(QWidget *parent) : QWidget(parent)
{
    //主要工作是设置界面
    mainL = new QGridLayout;
    buildL = new QGridLayout;
    showL = new QGridLayout;
    fileL = new QGridLayout;
    funcL = new QGridLayout;

    buildB.setText("build modal");
    buildSaveB.setText("save");
    buildCancleB.setText("cancle");

    showB.setText("show modal");
    showPathB.setText("show path");
    randMapB.setText("rand map");

    loadFromFile.setText("load from file");
    saveToFile.setText("save to file");

    pathNumLB.setText("path num:");
    pathNumValueLB.setNum(0);

    buildSizeSB.setRange(1, 50);
    buildSizeSB.setValue(15);
    randMapSB.setRange(1, 50);
    randMapSB.setValue(15);
    //randMapSB.setEnabled(false);
    pathNumSB.setRange(0, 0);
    pathNumSB.setValue(0);
    //pathNumSB.setEnabled(false);


    buildL->addWidget(&buildB, 1 ,1);
    buildL->addWidget(&buildSizeSB, 1, 2);
    buildL->addWidget(&buildSaveB, 2, 1);
    buildL->addWidget(&buildCancleB, 2, 2);
    buildL->setSpacing(3);

    showL->addWidget(&showB, 1, 1);
    showL->addWidget(&pathNumLB, 2, 1);
    showL->addWidget(&pathNumValueLB, 2, 2);
    showL->addWidget(&showPathB, 3, 1);
    showL->addWidget(&pathNumSB, 3, 2);
    showL->addWidget(&randMapB, 4, 1);
    showL->addWidget(&randMapSB, 4, 2);
    showL->setSpacing(3);

    fileL->addWidget(&loadFromFile, 1, 1);
    fileL->addWidget(&saveToFile, 2, 1);
    fileL->setSpacing(3);

    funcL->addLayout(buildL, 1 ,1);
    funcL->addLayout(showL, 2, 1);
    funcL->addLayout(fileL, 3, 1);
    funcL->setSpacing(30);

    funcW.setLayout(funcL);
    funcW.setFixedSize(funcW.sizeHint());

    mainL->addWidget(&mBoard, 1, 1);
    //mainL->addLayout(funcL, 1 ,2);
    mainL->addWidget(&funcW, 1, 2);
    mainL->setSpacing(5);

    this->setLayout(mainL);
    this->setFixedSize(this->sizeHint());

    //绑定信号槽
    QObject::connect(&buildB, SIGNAL(clicked()), this, SLOT(buildBClicked()));
    QObject::connect(&buildSaveB, SIGNAL(clicked()), this, SLOT(buildSaveBClicked()));
    QObject::connect(&buildCancleB, SIGNAL(clicked()), this, SLOT(buildCancleBClicked()));
    QObject::connect(&showB, SIGNAL(clicked()), this, SLOT(showBClicked()));
    QObject::connect(&showPathB, SIGNAL(clicked()), this, SLOT(showPathBClicked()));
    QObject::connect(&randMapB, SIGNAL(clicked()), this, SLOT(randMapBClicked()));
    QObject::connect(&loadFromFile, SIGNAL(clicked()), this, SLOT(loadFromFileCliked()));
    QObject::connect(&saveToFile, SIGNAL(clicked()), this, SLOT(saveToFileClicked()));

    //初始化为展示模式
    showBClicked();
}


int mazeMainWin::buildBClicked()
{
    showPathB.setEnabled(false);
    randMapB.setEnabled(false);
    randMapSB.setEnabled(false);
    pathNumSB.setEnabled(false);
    saveToFile.setEnabled(false);


    buildSaveB.setEnabled(true);
    buildCancleB.setEnabled(true);

    int buildSize = buildSizeSB.value();

    mBoard.setBuildModal(buildSize);

    mainL->addWidget(&mBoard, 1, 1);
    this->setFixedSize(this->sizeHint());
    return 1;


}

int mazeMainWin::buildSaveBClicked()
{
    mBoard.saveBuildResult();
    mBoard.freshBoard();
    showBClicked();
    return 1;
}

int mazeMainWin::buildCancleBClicked()
{
    showBClicked();
    return 1;
}

int mazeMainWin::showBClicked()
{
    //showPathB.setEnabled(true);
    showPathB.setEnabled(true);
    randMapB.setEnabled(true);
    randMapSB.setEnabled(true);
    pathNumSB.setEnabled(true);
    saveToFile.setEnabled(true);


    buildSaveB.setEnabled(false);
    buildCancleB.setEnabled(false);

    mBoard.setShowModal();

    //mBoard.freshBoard();

    maze* tmpMaze = mBoard.getMaze();
    tmpMaze->solveMap();

    //设置路径信息
    pathNumValueLB.setNum(tmpMaze->getPathNum());
    pathNumSB.setRange(0, tmpMaze->getPathNum());
    pathNumSB.setValue(0);
    showPathBClicked();

    mainL->addWidget(&mBoard, 1, 1);
    this->setFixedSize(this->sizeHint());

    return 1;
}

int mazeMainWin::showPathBClicked()
{
    mBoard.showPath(pathNumSB.value());
    return 1;

}


int mazeMainWin::randMapBClicked()
{
    maze* tmpMaze = mBoard.getMaze();
    tmpMaze->createRunableMap(randMapSB.value());
    tmpMaze->solveMap();
    mBoard.freshBoard();
    mainL->addWidget(&mBoard, 1, 1);

    pathNumValueLB.setNum(tmpMaze->getPathNum());
    pathNumSB.setRange(0, tmpMaze->getPathNum());
    pathNumSB.setValue(0);
    showPathBClicked();
    this->setFixedSize(this->sizeHint());
    return 1;
}

int mazeMainWin::loadFromFileCliked()
{
    QString fileNameQS = QFileDialog::getOpenFileName(this, tr("Open The Map"), "./");
    if(fileNameQS == 0)
    {
        return 0;
    }
    QByteArray fileNameBA = fileNameQS.toLatin1();
    char* fileName = fileNameBA.data();
    maze* tmpMaze = mBoard.getMaze();
    tmpMaze->setMap(fileName);
    showBClicked();
    return 1;
}

int mazeMainWin::saveToFileClicked()
{
    QString fileNameQS = QFileDialog::getSaveFileName(this, tr("Save The Map"), "./");
    if(fileNameQS == 0)
    {
        return 0;
    }
    QByteArray fileNameBA = fileNameQS.toLatin1();
    char* fileName = fileNameBA.data();
    maze* tmpMaze = mBoard.getMaze();
    tmpMaze->saveMap(fileName);
    return 1;
}
